<?php

require_once( '../system/smarty.inc' );

require_once( '../system/mdao/Class_M_DELIVE.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_INS_SHOP,$login['staff_auth']);

$dosearch = FALSE;
$dao      = new Class_M_DELIVE ;

$isG4  = FALSE;
if   ($login['staff_auth'] == 'G-4') {
	$isG4 = TRUE;
}
$isG5  = FALSE;
if   ($login['staff_auth'] == 'G-5') {
	$isG5 = TRUE;
}

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "copy" ) ) {
	$tWeek = $_GET['tweek'];
	$forms =  $_SESSION['MSEARCH']['MAP_SERVICE'];
}

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	$forms =  $_SESSION['MSEARCH']['MAP_SERVICE'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['MAP_SERVICE'] = $forms;
}
$wherearr =  array();


if ( isset( $forms['matrix_cd'] ) &&  ( !empty( $forms['matrix_cd'] ) ) ){
	$wherearr[] = sprintf(" matrix_cd =  '%s' ",  $forms['matrix_cd'] );
}

if ( isset( $forms['ins_co_cd'] ) &&  ( !empty( $forms['ins_co_cd'] ) ) ){
	$wherearr[] = sprintf(" ins_co_cd =  '%s' ",  $forms['ins_co_cd'] );
}

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "copy" ) ){
	 if ( $dao->copy($wherearr ,$tWeek ) ) {
	 	$copyMsg = "コピー成功しました。";
	 } else {
	 	$copyMsg = "コピー失敗しました。";
	 }
}

//---CSVダウンロード　処理開始
if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
$_GET["csv"] = "";
	$tmpfilename = "delive".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	$data = $dao->search( $wherearr );
//列名
	$colums = array();

//1 string code 2 integer 3 date 4 string ""必要

 
	$col_heads = array(
		'matrix_cd'    => array( 'マトリックスコード', 1),
		'year4'         => array( '年度',             1),
		'week_cd'      => array( '週',               1),
		'flier_date'   => array( '折込日',           1),
		'ins_co_cd'    => array( '折込会社コード',   1),
		'ins_co_name'  => array( '折込会社名',       1),
		'delive_cd'    => array( '納品先コード',     1),
		'delive_name'  => array( '納品先',           1),
		'close_day'    => array( '折込日前日数',     1),
		'close_time'   => array( '締切時間',         1),
		'postcode'     => array( '郵便番号',         1),
		'address'      => array( '住所',             1),
		'tel'          => array( '電話番号',         1),
		'u_time'       => array( '更新日時',         3),
		'c_time'       => array( '登録日時',         3),
		'm_user_name'  => array( '更新者',           1)
	);

	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	$week = Class_M_OTHER::get_by_matrix_cd($forms['matrix_cd'] );
	$week["flier_date"] =  Class_PWD::int8_to_dateD($week["flier_date"]);
	//データ整形開始
	foreach ($data as $k => $v) {
		$data[$k]["c_time"]      =  Class_PWD::dateTime_to_YmdHi($v["c_time"]);
		$data[$k]["u_time"]      =  Class_PWD::dateTime_to_YmdHi($v["u_time"]);
		$data[$k]["year4"]       =  $week["year4"];
		$data[$k]["week_cd"]     =  $week["week_cd"];
		$data[$k]["flier_date"]  =  $week["flier_date"];
	}
	//データ整形完了
	$dao->output_csv($data, $colums);
	exit;
}
//---CSVダウンロード　処理完了

if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
	$dosearch = TRUE;
}

if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
	$dosearch = TRUE;
	$page['current'] = $forms['page'];	
}else{
	$page['current'] = 1;
}
$page['item'] = PAGE_MAX;
if ($dosearch) {
	//$page内容は関数内部更新されます。
	$data = $dao->search( $wherearr, $page );
}
if (empty($forms["year4"])) {
	$forms["year4"] = date('Y');
}

if (empty($forms["year4c"])) {
	$forms["year4c"] = date('Y');
}
$ins_co = Class_M_OTHER::get_ins_co_array();
if ($isG4){
	$forms["ins_co_cd"]    = $_SESSION['ADMIN_LOGIN']['STAFF_INS_CO_CD'] ;
	$forms["ins_co_name"]  = $ins_co[$forms["ins_co_cd"]];
	$ins_co =array( $forms["ins_co_cd"] =>$forms["ins_co_name"] );
} else {
	$select = array(''=>PLEASE_SELECT_ARR);
	
	$ins_co = array_merge($select, $ins_co);
}
$smarty->assign('isG4', $isG4 );
$smarty->assign('ins_co', $ins_co );

if( isset($forms) ) $smarty->assign('forms', $forms);
if( isset($data) )  $smarty->assign('data', $data);
if( isset($page) )  $smarty->assign('page', $page);
if( isset($copyMsg)) $smarty->assign('copymsg', $copyMsg );

$smarty->assign('year4',  Class_PWD::get_years());

$week = Class_M_OTHER::get_week_array( $forms["year4"], PLEASE_SELECT_ARR );
$smarty->assign('week', $week );

$weekc = Class_M_OTHER::get_week_array( $forms["year4c"], PLEASE_SELECT_ARR );
$smarty->assign('weekc', $weekc );

$smarty->assign('menu',  $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('mins_delive_index.html');

?>